package sbu.cs.ui.widget;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import sbu.cs.IMessageObserver;
import sbu.cs.R;
import sbu.cs.Settings;
import sbu.cs.asdb.ASDBUtils;

import android.content.Context;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Spinner;

public class MessagesView extends LinearLayout implements OnItemClickListener {

	private ListView listViewMessages;
	private List<String> messages;
	private ArrayAdapter<String> adapter;
	private Spinner spinnerOptions;
	private List<HashMap<String, String>> hashMaps;
	private List<HashMap<String, String>> originalHashMaps;
	private IMessageObserver messageObserver;

	private int sort;

	// String lv_arr[]={"Android","iPhone","BlackBerry","AndroidPeople"};
	// listView.setAdapter(new ArrayAdapter<String>(context,
	// android.R.layout.simple_list_item_1, lv_arr));

	public MessagesView(Context context) {
		super(context);
		init(context);
	}

	private void init(Context context) {
		setOrientation(LinearLayout.VERTICAL);

		spinnerOptions = new Spinner(context);
		spinnerOptions.setPromptId(R.string.spinner_options_prompt);
		ArrayAdapter<CharSequence> adapterSpinner = ArrayAdapter
				.createFromResource(context, R.array.options_sort,
						android.R.layout.simple_spinner_item);
		adapterSpinner
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinnerOptions.setAdapter(adapterSpinner);

		spinnerOptions.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view,
					int pos, long id) {
				sort = pos;
				switch (pos) {
				case 0:
					sortMessagesByTime();
					break;
				case 1:
					sortMessagesByPriority();
					break;
				case 2:
					sortMessagesByCategory();
					break;
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				switch (sort) {
				case 0:
					sortMessagesByTime();
					break;
				case 1:
					sortMessagesByPriority();
					break;
				case 2:
					sortMessagesByCategory();
					break;
				}
			}
		});
		addView(spinnerOptions);

		listViewMessages = new ListView(context);
		messages = new ArrayList<String>();
		adapter = new ArrayAdapter<String>(context,
				android.R.layout.simple_list_item_1, messages);
		listViewMessages.setAdapter(adapter);
		listViewMessages.setOnItemClickListener(this);
		addView(listViewMessages);
		
		originalHashMaps = new ArrayList<HashMap<String, String>>(); 
		
	}

	private void sortMessagesByCategory() {
		messages.clear();
		List<String> alerts = new ArrayList<String>();
		List<String> academic = new ArrayList<String>();
		List<String> shopping = new ArrayList<String>();
		List<String> sports = new ArrayList<String>();
		List<String> food = new ArrayList<String>();
		List<String> fun = new ArrayList<String>();

		List<HashMap<String, String>> alertsMap = new ArrayList<HashMap<String, String>>();
		List<HashMap<String, String>> academicMap = new ArrayList<HashMap<String, String>>();
		List<HashMap<String, String>> shoppingMap = new ArrayList<HashMap<String, String>>();
		List<HashMap<String, String>> sportsMap = new ArrayList<HashMap<String, String>>();
		List<HashMap<String, String>> foodMap = new ArrayList<HashMap<String, String>>();
		List<HashMap<String, String>> funMap = new ArrayList<HashMap<String, String>>();

		for (HashMap<String, String> map : hashMaps) {
			if (map.get(ASDBUtils.ATTR_CATEGORY).equals(
					Settings.CATEGORY_ALERTS)) {
				alerts.add(map.get(ASDBUtils.ATTR_UID) + "\n\t"
						+ map.get(ASDBUtils.ATTR_SUBJECT));
				alertsMap.add(map);
			} else if (map.get(ASDBUtils.ATTR_CATEGORY).equals(
					Settings.CATEGORY_ACADEMIC)) {
				academic.add(map.get(ASDBUtils.ATTR_UID) + "\n\t"
						+ map.get(ASDBUtils.ATTR_SUBJECT));
				academicMap.add(map);
			} else if (map.get(ASDBUtils.ATTR_CATEGORY).equals(
					Settings.CATEGORY_SHOPPING)) {
				shopping.add(map.get(ASDBUtils.ATTR_UID) + "\n\t"
						+ map.get(ASDBUtils.ATTR_SUBJECT));
				shoppingMap.add(map);
			} else if (map.get(ASDBUtils.ATTR_CATEGORY).equals(
					Settings.CATEGORY_SPORTS)) {
				sports.add(map.get(ASDBUtils.ATTR_UID) + "\n\t"
						+ map.get(ASDBUtils.ATTR_SUBJECT));
				sportsMap.add(map);
			} else if (map.get(ASDBUtils.ATTR_CATEGORY).equals(
					Settings.CATEGORY_FOOD)) {
				food.add(map.get(ASDBUtils.ATTR_UID) + "\n\t"
						+ map.get(ASDBUtils.ATTR_SUBJECT));
				foodMap.add(map);
			} else if (map.get(ASDBUtils.ATTR_CATEGORY).equals(
					Settings.CATEGORY_FUN)) {
				fun.add(map.get(ASDBUtils.ATTR_UID) + "\n\t"
						+ map.get(ASDBUtils.ATTR_SUBJECT));
				funMap.add(map);
			}
			// messages.add(map.get(ASDBUtils.ATTR_UID) + "\n\t" +
			// map.get(ASDBUtils.ATTR_SUBJECT));
			// adapter.notifyDataSetChanged();
		}

		for (String msg : alerts) {
			messages.add(msg);
		}
		for (String msg : academic) {
			messages.add(msg);
		}
		for (String msg : shopping) {
			messages.add(msg);
		}
		for (String msg : sports) {
			messages.add(msg);
		}
		for (String msg : food) {
			messages.add(msg);
		}
		for (String msg : fun) {
			messages.add(msg);
		}
		hashMaps.clear();

		for (HashMap<String, String> map : alertsMap) {
			hashMaps.add(map);
		}
		for (HashMap<String, String> map : academicMap) {
			hashMaps.add(map);
		}
		for (HashMap<String, String> map : shoppingMap) {
			hashMaps.add(map);
		}
		for (HashMap<String, String> map : sportsMap) {
			hashMaps.add(map);
		}
		for (HashMap<String, String> map : foodMap) {
			hashMaps.add(map);
		}
		for (HashMap<String, String> map : funMap) {
			hashMaps.add(map);
		}
		adapter.notifyDataSetChanged();
	}

	private void sortMessagesByPriority() {
		messages.clear();
		List<String> critical = new ArrayList<String>();
		List<String> high = new ArrayList<String>();
		List<String> medium = new ArrayList<String>();
		List<String> low = new ArrayList<String>();

		List<HashMap<String, String>> criticalMap = new ArrayList<HashMap<String, String>>();
		List<HashMap<String, String>> highMap = new ArrayList<HashMap<String, String>>();
		List<HashMap<String, String>> mediumMap = new ArrayList<HashMap<String, String>>();
		List<HashMap<String, String>> lowMap = new ArrayList<HashMap<String, String>>();

		for (HashMap<String, String> map : hashMaps) {
			if (map.get(ASDBUtils.ATTR_PRIORITY).equals(
					Settings.PRIORITY_CRITICAL)) {
				critical.add(map.get(ASDBUtils.ATTR_UID) + "\n\t"
						+ map.get(ASDBUtils.ATTR_SUBJECT));
				criticalMap.add(map);
			} else if (map.get(ASDBUtils.ATTR_PRIORITY).equals(
					Settings.PRIORITY_HIGH)) {
				high.add(map.get(ASDBUtils.ATTR_UID) + "\n\t"
						+ map.get(ASDBUtils.ATTR_SUBJECT));
				highMap.add(map);
			} else if (map.get(ASDBUtils.ATTR_PRIORITY).equals(
					Settings.PRIORITY_MEDIUM)) {
				medium.add(map.get(ASDBUtils.ATTR_UID) + "\n\t"
						+ map.get(ASDBUtils.ATTR_SUBJECT));
				mediumMap.add(map);
			} else if (map.get(ASDBUtils.ATTR_PRIORITY).equals(
					Settings.PRIORITY_LOW)) {
				low.add(map.get(ASDBUtils.ATTR_UID) + "\n\t"
						+ map.get(ASDBUtils.ATTR_SUBJECT));
				lowMap.add(map);
			}
			// messages.add(map.get(ASDBUtils.ATTR_UID) + "\n\t" +
			// map.get(ASDBUtils.ATTR_SUBJECT));
			// adapter.notifyDataSetChanged();
		}

		for (String msg : critical) {
			messages.add(msg);
		}
		for (String msg : high) {
			messages.add(msg);
		}
		for (String msg : medium) {
			messages.add(msg);
		}
		for (String msg : low) {
			messages.add(msg);
		}

		hashMaps.clear();

		for (HashMap<String, String> map : criticalMap) {
			hashMaps.add(map);
		}
		for (HashMap<String, String> map : highMap) {
			hashMaps.add(map);
		}
		for (HashMap<String, String> map : mediumMap) {
			hashMaps.add(map);
		}
		for (HashMap<String, String> map : lowMap) {
			hashMaps.add(map);
		}
		adapter.notifyDataSetChanged();
	}

	private void sortMessagesByTime() {
		if (originalHashMaps != null) {
			messages.clear();
			hashMaps = originalHashMaps;
			for (HashMap<String, String> map : hashMaps) {
				messages.add(map.get(ASDBUtils.ATTR_UID) + "\n\t"
						+ map.get(ASDBUtils.ATTR_SUBJECT));
			}
		}
		adapter.notifyDataSetChanged();
	}

	public void setMessagesList(List<HashMap<String, String>> msgs) {
		messages.clear();
		hashMaps = msgs;
		originalHashMaps.clear();
		for (HashMap<String, String> map : hashMaps) {
			originalHashMaps.add(map);
		}
		for (HashMap<String, String> map : msgs) {
			messages.add(map.get(ASDBUtils.ATTR_UID) + "\n\t"
					+ map.get(ASDBUtils.ATTR_SUBJECT));
			adapter.notifyDataSetChanged();
		}
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		HashMap<String, String> map = hashMaps.get(position);
		String summary = "";
		summary += "\n\tFrom:\t\t\t" + map.get(ASDBUtils.ATTR_UID) + "\n\n\t"
				+ "Subject:\t\t" + map.get(ASDBUtils.ATTR_SUBJECT) + "\n\n\t"
				+ "Message:\t" + map.get(ASDBUtils.ATTR_MESSAGE) + "\n\n\t"
				+ "Category:\t" + map.get(ASDBUtils.ATTR_CATEGORY) + "\n\n\t"
				+ "Priority:\t\t" + map.get(ASDBUtils.ATTR_PRIORITY) + "\n\t";
		messageObserver.onReceive(summary);
	}

	public void setMessageObserver(IMessageObserver observer) {
		messageObserver = observer;
	}
}
